/**
 * PK数据
 *
 * created by muming
 * 2016年05月19日
 */
'use strict';

const builder = require('../filterBuilder');
const api = require('../dicAPIUrl');
const fillToMapHandler = require('../util').fillToMapHandler;

/**
 * @public
 * 解析请求数据
 *
 * @param (object) requestData 请求数据
 * @return 请求参数
 */
function request(requestData) {
	let filters = filter(requestData.condition);
	let timeUnit = requestData.currentTimeUnit;

    // PK人数
    let pkPeopleCount = {
		"data": {
			"target": "hurdle_record-student_id",
			"data_type": "count",
			"type": "normal"
		},
		"time_unit": timeUnit,
		"multi_unit": "",
		"filter": [{
				"item": "hurdle_record-play_type",
				"type": "=",
				"value": 2	// 1：闯关；2：PK
			}
		].concat(filters)
	};

	// PK次数
	let pkTimeCount = {
		"data": {
			"target": "hurdle_record-hurdle_record_id",
			"data_type": "count",
			"type": "normal"
		},
		"time_unit": timeUnit,
		"multi_unit": "",
		"filter": [{
				"item": "hurdle_record-play_type",
				"type": "=",
				"value": 2	// 1：闯关；2：PK
			}
		].concat(filters)
	};

	// 关卡数
	let pkMissionCount = {
		"data": {
			"target": "hurdle_record-hurdle_id",
			"data_type": "count",
			"type": "normal"
		},
		"time_unit": timeUnit,
		"multi_unit": "",
		"filter": [{
				"item": "hurdle_record-play_type",
				"type": "=",
				"value": 2	// 1：闯关；2：PK
			}
		].concat(filters)
	};

    return [pkPeopleCount, pkTimeCount, pkMissionCount];
}

/**
 * @public
 * 解析响应数据
 */
function response(pkPeopleCount, pkTimeCount, pkMissionCount) {
    let set = new Set();
    let handler = fillToMapHandler.bind(set);

    let pkPeopleCountMap = handler(pkPeopleCount);
    let pkTimeCountMap = handler(pkTimeCount);
    let pkMissionCountMap = handler(pkMissionCount);

	let rn = {
        categories: Array.from(set),
        pkPeopleCountSeries: [],
        pkTimeCountSeries: [],
        pkMissionCountSeries: []
    };

    rn.categories.forEach((item) => {
        rn.pkPeopleCountSeries.push(+pkPeopleCountMap[item] || 0);
        rn.pkTimeCountSeries.push(+pkTimeCountMap[item] || 0);
        rn.pkMissionCountSeries.push(+pkMissionCountMap[item] || 0);
    });

    return rn;
}

/**
 * @private
 * 解析过滤条件
 *
 * @param (object) condition 请求数据中的condition字段
 * @return 过滤条件数组
 */
function filter(condition) {
	let filters = [];

    if (condition.province && condition.province != builder.NIL) {
        filters.push(builder.append('student-province_id', '=', condition.province));
    }
    if (condition.city && condition.city != builder.NIL) {
        filters.push(builder.append('student-city_id', '=', condition.city));
    }
    if (condition.county && condition.county != builder.NIL) {
        filters.push(builder.append('student-county_id', '=', condition.county));
    }
    if (condition.school && condition.school != builder.NIL) {
        filters.push(builder.append('student-school_id', '=', condition.school));
    }
    if (condition.stage[0] != builder.NIL) {
        filters.push(builder.append('student-grade_part', 'in', condition.stage.join(',')));
    }
    if (condition.grade[0] != builder.NIL) {
        filters.push(builder.append('student-grade', 'in', condition.grade.join(',')));
    }
    if (condition.subject[0] != builder.NIL) {
        filters.push(builder.append('hurdle_student_ext-hurdle_subject', 'in', condition.subject.join(',')));
    }
    if (condition.version != '0') {
        filters.push(builder.append('student-version', '=', condition.version));
    }
    if (condition.deviceType[0] != builder.NIL) {
        filters.push(builder.append('student-source', 'in', condition.deviceType.join(',')));
    }

    filters.push(builder.append('hurdle_record-day', '>=', condition.startDate));
    filters.push(builder.append('hurdle_record-day', '<=', condition.endDate));
    filters.push(builder.append('student-teacher_student_count', '>=', condition.userFeature));

    return filters;
}

module.exports = {
    API_URL: api.API_PACKAGE_DATA,
    request,
    response
};
